package com.yc.sole.erp.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yc.sole.erp.dto.MoldDto;
import com.yc.sole.erp.entity.Mold;
import com.yc.sole.erp.mapper.MoldMapper;
import com.yc.sole.erp.request.MoldQueryReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 模具表 服务接口实现
 *
 * @author yizuomin
 * @date 2023-05-07
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class MoldRepository extends ServiceImpl<MoldMapper, Mold> {

    /**
     * 新增模具表
     *
     * @param moldDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveMold(MoldDto moldDto) {
        Mold entity = new Mold();
        BeanUtil.copyProperties(moldDto, entity);
        this.save(entity);
    }

    /**
     * 批量新增模具表
     *
     * @param moldDtos
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveMold(Collection<MoldDto> moldDtos) {
        List<Mold> moldList = BeanUtil.copyToList(moldDtos, Mold.class);
        this.saveBatch(moldList);
    }

    /**
     * 通过Id修改模具表
     *
     * @param moldDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateMoldById(MoldDto moldDto) {
        Mold entity = new Mold();
        BeanUtil.copyProperties(moldDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id删除模具表
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteMoldByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取模具表
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public List<MoldDto> selectMoldByIds(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), MoldDto.class);
    }

    /**
     * 根据id获取模具表
     *
     * @param id
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public MoldDto selectMoldById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), MoldDto.class);
    }

    /**
     * 查询集合
     *
     * @param moldQueryReq
     * @return
     */
    public List<MoldDto> selectMoldList(MoldQueryReq moldQueryReq) {
        LambdaQueryWrapper<Mold> queryWrapper = this.getQueryWrapper(moldQueryReq);
        return BeanUtil.copyToList(this.list(queryWrapper), MoldDto.class);
    }

    /**
     * 查询集合
     *
     * @param moldNames
     * @return
     */
    public List<MoldDto> selectMoldListByMoldNames(Set<String> moldNames) {
        List<List<String>> nameList = Lists.partition(moldNames.stream().toList(), 1000);
        Set<Mold> moldList = nameList.stream().map(supplierNames -> {
            LambdaQueryWrapper<Mold> wrapper = Wrappers.<Mold>lambdaQuery()
                    .select(Mold::getMoldName)
                    .in(Mold::getMoldName, supplierNames);
            return this.list(wrapper);
        }).flatMap(Collection::stream).collect(Collectors.toSet());
        return BeanUtil.copyToList(moldList, MoldDto.class);
    }

    /**
     * 根据名称查询集合
     *
     * @param moldName
     * @return
     */
    public List<MoldDto> selectMoldByName(String moldName) {
        LambdaQueryWrapper<Mold> wrapper = Wrappers.<Mold>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(moldName), Mold::getMoldName, moldName);
        Page<Mold> moldPage = new Page<>(1, 10);
        return BeanUtil.copyToList(this.page(moldPage, wrapper).getRecords(), MoldDto.class);
    }

    /**
     * 分页查询模具表
     *
     * @param page
     * @return RestResult
     */
    public Paging<MoldDto> selectMoldPage(Paging page, MoldQueryReq moldPageReq) {
        LambdaQueryWrapper<Mold> queryWrapper = this.getQueryWrapper(moldPageReq);
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), MoldDto.class);
    }

    /**
     * 查询条件组装
     *
     * @param moldPageReq
     * @return
     */
    private LambdaQueryWrapper<Mold> getQueryWrapper(MoldQueryReq moldPageReq) {
        return Wrappers.<Mold>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(moldPageReq.getMoldName()), Mold::getMoldName, moldPageReq.getMoldName())
                .orderByDesc(Mold::getId);
    }
}
